package com.jhj.common.interceptor;

import static com.jhj.common.constants.CookieConstants.EMAIL;
import static com.jhj.common.constants.CookieConstants.ID;
import static com.jhj.common.constants.CookieConstants.LOGIN_COOKIE_AGE;
import static com.jhj.common.constants.CookieConstants.LOGIN_NICK_NAME;
import static com.jhj.common.constants.CookieConstants.LOGIN_STATUS;
import static com.jhj.common.constants.CookieConstants.LOGIN_TIME;
import static com.jhj.common.constants.CookieConstants.LOGIN_TIME_FOMART;
import static com.jhj.common.constants.CookieConstants.LOGIN_USER_NAME;
import static com.jhj.common.constants.CookieConstants.MOBILE_PHONE;
import static com.jhj.common.constants.CookieConstants.NICK_NAME;
import static com.jhj.common.constants.CookieConstants.USER_NAME;
import static com.jhj.common.constants.CookieConstants.WEB_EMAIL;
import static com.jhj.common.constants.CookieConstants.WEB_GBT;
import static com.jhj.common.constants.CookieConstants.WEB_ID;
import static com.jhj.common.constants.CookieConstants.WEB_LOGIN_TIME;
import static com.jhj.common.constants.CookieConstants.WEB_MOBILE_PHONE;
import static com.jhj.common.constants.CookieConstants.WEB_NICK_NAME;
import static com.jhj.common.constants.CookieConstants.WEB_USER_NAME;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.jhj.common.util.JhjCookieUtil;
import com.jhj.common.util.JhjEncryptUtil;
import com.jhj.common.util.JhjTimeUtil;

public class JhjCookieInterceptor extends HandlerInterceptorAdapter {
    
    private String secretKey;
    
    public void setSecretKey(String secretKey) {
        this.secretKey = secretKey;
    }
    
    @Override
    public boolean preHandle(HttpServletRequest request, 
            HttpServletResponse response, Object handler) throws Exception {
        String id = JhjCookieUtil.getValue(WEB_ID, request);
        String userName = JhjCookieUtil.getValue(WEB_USER_NAME, request);
        String nickName = JhjCookieUtil.getValue(WEB_NICK_NAME, request);
        String mobilePhone = JhjCookieUtil.getValue(WEB_MOBILE_PHONE, request);
        String email = JhjCookieUtil.getValue(WEB_EMAIL, request);
        String loginTime = JhjCookieUtil.getValue(WEB_LOGIN_TIME, request);
        /** 登陆成功后加密后的数据字符串 */
        String gbt = JhjCookieUtil.getValue(WEB_GBT, request);
        
        Map<String, String> paramsMap = new HashMap<String, String>();
        paramsMap.put(ID, id);
        paramsMap.put(USER_NAME, userName);
        paramsMap.put(NICK_NAME, nickName);
        paramsMap.put(MOBILE_PHONE, mobilePhone);
        paramsMap.put(EMAIL, email);
        paramsMap.put(LOGIN_TIME, loginTime);
        
        String sign = JhjEncryptUtil.getSign(paramsMap, secretKey);
        int day = Integer.MAX_VALUE;
        
        JhjTimeUtil timeUtil = JhjTimeUtil.getInstance();
        Date loginDateTime = timeUtil.getDateFromString(loginTime, LOGIN_TIME_FOMART);
        
        if (loginDateTime != null) {
            day = timeUtil.intervalDays(loginDateTime, new Date());
        }
        
        // 加密字符串一致，且为三天之内输入密码登录成功
        if (sign.equals(gbt) && day <= LOGIN_COOKIE_AGE) {
            // 登录用户
            request.setAttribute(LOGIN_STATUS, true);
            request.setAttribute(LOGIN_USER_NAME, userName);
            request.setAttribute(LOGIN_NICK_NAME, nickName);
        } else {
            request.setAttribute(LOGIN_STATUS, false);
        }
        
        return super.preHandle(request, response, handler);
    }
}
